<template>
  <div class="app-container">

    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane v-for="type in platformOption"
                   :label="type.dictLabel"
                   :name="type.dictValue + ''">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="用户ID" prop="id">
            <el-input v-model="queryParams.id" placeholder="请输入用户ID" clearable size="mini"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item label="用户昵称" prop="carMaxCount">
            <el-input v-model="queryParams.nickName" placeholder="请输入用户昵称" clearable size="mini"
              @keyup.enter.native="handleQuery"/>
          </el-form-item>

          <el-form-item label="注册时间" prop="propertyName">
            <el-date-picker v-model="dateRange" size="mini" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                            range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
    <!--      <el-col :span="1.5">-->
    <!--        <router-link :to="'/user/promoter?groupId='+this.groupId" class="link-type">-->
    <!--          <el-button icon="el-icon-back" size="mini">-->
    <!--            返 回-->
    <!--          </el-button>-->
    <!--        </router-link>-->
    <!--      </el-col>-->
          <el-col :span="1.5">
            <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                       v-hasPermi="['module:wxuser:export']">导出用户数据</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="用户ID" align="center" prop="id" />
          <el-table-column label="用户昵称" align="center" prop="nickName"/>
          <el-table-column label="绑定手机号" align="center" prop="phoneNum"/>
    <!--      <el-table-column label="绑定小区" align="center" prop="estateName" width="130">-->
    <!--        <template slot-scope="scope">-->
    <!--          <div>-->
    <!--            <el-tag style="cursor: pointer" v-if="scope.row.estateName !== null" :type="''">{{scope.row.estateName}}</el-tag>-->
    <!--            <el-tag style="cursor: pointer" v-else :type="'warning'">未绑定小区</el-tag>-->
    <!--          </div>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="所在地区" align="center" prop="address" width="100">-->
    <!--        <template slot-scope="scope">-->
    <!--          <div v-if="scope.row.address == 1">日照</div>-->
    <!--          <div v-if="scope.row.address == 2">外地</div>-->
    <!--          <div v-if="scope.row.address == 0">未知</div>-->
    <!--        </template>-->
    <!--      </el-table-column>-->
          <el-table-column label="用户头像" align="center">
            <template slot-scope="scope">
              <a :href="scope.row.linkLogo" style="color: #42b983" target="_blank">
                <img :src="scope.row.faceUrl" alt="点击打开" class="el-avatar">
              </a>
            </template>
          </el-table-column>
          <el-table-column label="注册时间" align="center" prop="createTime"/>
          <el-table-column label="最后登录时间" align="center" prop="lastActiveTime"/>
    <!--      <el-table-column label="微信新邀请" align="center" prop="inviteUserCount">-->
    <!--        <template slot-scope="scope">-->
    <!--          {{scope.row.inviteUserCount+'人'}}-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="微信邀请" align="center" prop="inviteUserCountTotal">-->
    <!--        <template slot-scope="scope">-->
    <!--          {{scope.row.inviteUserCountTotal+'人'}}-->
    <!--        </template>-->
    <!--      </el-table-column>-->
    <!--      <el-table-column label="平台积分" align="center" prop="integral" />-->
        </el-table>


        <pagination v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"/>
      </el-tab-pane>
    </el-tabs>

    <!-- 添加或修改微信端用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" size="mini"/>
        </el-form-item>
        <el-form-item label="昵称" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入昵称" size="mini"/>
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNum">
          <el-input v-model="form.phoneNum" placeholder="请输入手机号" size="mini"/>
        </el-form-item>
        <el-form-item label="头像地址" prop="faceUrl">
          <el-input v-model="form.faceUrl" placeholder="请输入头像地址" size="mini"/>
        </el-form-item>
        <el-form-item label="所处省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入所处省份" size="mini"/>
        </el-form-item>
        <el-form-item label="所处城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入所处城市" size="mini"/>
        </el-form-item>
        <el-form-item label="所处城市" prop="sessionKey">
          <el-input v-model="form.sessionKey" placeholder="请输入所处城市" size="mini"/>
        </el-form-item>
        <el-form-item label="最后的登录ip" prop="lastLoginIp">
          <el-input v-model="form.lastLoginIp" placeholder="请输入最后的登录ip" size="mini"/>
        </el-form-item>
        <el-form-item label="用户购买成功的次数" prop="buyCount">
          <el-input v-model="form.buyCount" placeholder="请输入用户购买成功的次数" size="mini"/>
        </el-form-item>
        <el-form-item label="是否是平台管理员" prop="isAdmin">
          <el-input v-model="form.isAdmin" placeholder="请输入是否是平台管理员" size="mini"/>
        </el-form-item>
        <el-form-item label="积分" prop="integral">
          <el-input v-model="form.integral" placeholder="请输入积分" size="mini"/>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别" size="mini">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listUser, getUser, delUser, addUser, updateUser, exportUser,} from "@/api/module/wx/user";

export default {
  name: "User",
  data() {
    return {
      activeName:'wxmini',
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      groupId:undefined,
      // 总条数
      total: 0,
      // 微信端用户表格数据
      userList: [],
      dateRange:[],
      platformOption:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id:undefined,
        nickName: undefined,
        platform:undefined,
        beginDate:undefined,
        endDate:undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    console.log('section  this.$route.query',this.$route.query);
    this.getList();

    this.getDicts("wx_platform").then(response => {
      this.platformOption = response.data;
    });
  },
  methods: {
    /** 查询微信端用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    handleClick(val){
      console.log('handleClick',val);
      this.activeName = val.name;
      this.queryParams.pageNum = 1;
      this.queryParams.platform = val.name;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userNo: undefined,
        platform:undefined,
        nickName: undefined,
        realName: undefined,
        phoneNum: undefined,
        faceUrl: undefined,
        province: undefined,
        city: undefined,
        sex: undefined,
        sessionKey: undefined,
        createTime: undefined,
        lastActiveTime: undefined,
        lastLoginIp: undefined,
        openId: undefined,
        unionId: undefined,
        vipLevel: undefined,
        userType: undefined,
        buyCount: undefined,
        shopId: undefined,
        isAdmin: undefined,
        sysUserId: undefined,
        integral: undefined,
        inviteUserCount:undefined,
        inviteUserCountTotal:undefined,
        inviteUserCountTotal1:undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log('dateRange',this.dateRange);
      if(this.dateRange !=undefined && this.dateRange.length === 2){
        this.queryParams.beginDate = this.dateRange[0];
        this.queryParams.endDate = this.dateRange[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微信端用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getUser(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改微信端用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addUser(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除微信端用户编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delUser(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出邀请数据操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有平台用户数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportUser(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
